var tableIns;
layui.extend({
    dtree: '/layui_ext/dist/dtree'
}).use(['jquery', 'layer', 'form', 'table', 'dtree'], function () {
    var $ = layui.jquery,
        layer = layui.layer,
        form = layui.form,
        table = layui.table,
        dtree = layui.dtree;

    tableIns = table.render({
        elem: '#roleTable',
        url: '/system/selectAllRole',
        title: '角色数据表',
        toolbar: '#roleToolBar',
        // height: 'full-230',
        cellMinWidth: 200,
        page: 1,
        limits: [10, 20, 30],
        limit: 10,
        cols: [[
            {field: 'roleid', title: 'ID', align: 'center', width: '50'},
            {field: 'rolename', title: '角色姓名', align: 'center', width: '80'},
            {field: 'roledesc', title: '角色备注', align: 'center', width: '150'},
            {
                field: 'available', title: '状态', align: 'center', templet: function (d) {
                    return d.available == '1' ? '<font color="blue">可用</font>' : '<font color="red">不可用</font>';
                }
            },
            {fixed: 'right', title: '操作', toolbar: '#roleBar', align: 'center', width: '260'}
        ]]
    });

    $("#doSearch").click(function () {
        var params = $("#searchFrm").serialize();
        tableIns.reload({
            url: '/system/selectAllRole?' + params
        });
    });

    table.on("toolbar(roleTable)", function (obj) {
        switch (obj.event) {
            case 'add':
                openAddRole();
                break;
        }
    });

    table.on('tool(roleTable)', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent == 'del') {
            layer.confirm('确认删除【' + data.rolename + '】角色吗？', function () {
                $.post("/system/deleteRole", {roleid: data.roleid}, function (res) {
                    layer.msg(res);
                    tableIns.reload();
                });
            });
        } else if (layEvent == 'edit') {
            openUpdateRole(data);
        } else if (layEvent == 'selectRoleMenu') {
            openselectRoleMenu(data);
        }
    });

    var flag = false;

    function rolename() {
        $.post('/system/queryRoleName', {rolename: $("#rolename").val()}, function (res) {
            if (res > 0) {
                layer.msg("角色名称已存在！");
                flag = true;
            } else {
                flag = false;
            }
        });
    }

    var url;
    var mainIndex;

    function openAddRole() {
        if (flag == true) {
            layer.msg("角色名称已存在！");
            return false;
        }
        mainIndex = layer.open({
            type: 1,
            title: '添加角色',
            content: $("#saveOrUpdateDiv"),
            area: ['800px', '400px'],
            success: function (index) {
                $("#dataFrm")[0].reset();
                url = "/system/addRole";
            }
        });
    }

    function openUpdateRole(data) {
        mainIndex = layer.open({
            type: 1,
            title: '修改角色',
            content: $("#saveOrUpdateDiv"),
            area: ['800px', '400px'],
            success: function (index) {
                form.val("dataFrm", data);
                url = "/system/updateRole";
            }
        });
    }

    form.on('submit(doSubmit)', function () {
        var params = $("#dataFrm").serialize();
        $.post(url, params, function (res) {
            layer.msg(res);
            layer.close(mainIndex);
            tableIns.reload();
        });
    });

    function openselectRoleMenu(data) {
        var menuTree;
        mainIndex = layer.open({
            type: 1,
            title: '分配【' + data.rolename + '】角色的菜单',
            content: $("#selectRoleMenu"),
            area: ['400px', '500px'],
            btnAlign: 'c',
            height: 'full',
            btn: ['<div class="layui-icon layui-icon-release">确认分配</div>',
                '<div class="layui-icon layui-icon-close">取消分配</div>'],
            yes: function (index, layero) {
                var nodes = dtree.getCheckbarNodesParam("menuTree");
                var params = "roleid=" + data.roleid;
                $.each(nodes, function (i, item) {
                    params += "&ids=" + item.nodeId;
                });

                $.post("/system/saveRoleMenu", params, function (res) {
                    if (res > 0) {
                        layer.msg("角色权限分配成功！");
                    } else {
                        layer.msg("角色权限分配失败！");
                    }

                    layer.close(mainIndex);
                    tableIns.reload();
                });
            },
            success: function (index) {
                mainIndex = dtree.render({
                    elem: '#menuTree',
                    dataStyle: "layuiStyle",
                    url: '/system/initRoleMenu?roleid=' + data.roleid,
                    response: {message: "msg", statusCode: 0},
                    dataFormat: "list",
                    checkbar: true,
                    checkbarData: "choose"
                });
            }
        });
    }
});